/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableBannerHourV2 {
    public static final String KEYSPACE                         =   "AdsData";
    public static final String ColumFamly                       =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf     =  "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf      =  "BannerViewCountCf";

    public void handleData() throws ParseException, IOException
    {
        long first_time = System.currentTimeMillis();
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);
        MysqlDataManager67 mysql67 = new MysqlDataManager67();
        //MysqlDataManager mysqldatamanager = new MysqlDataManager();
        
        List listbanners = mysql67.getBannerIDByCPCandCPCPlus();
        //String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
        String currentDate = HandleDataTableDomainDate.handleDate();//2012-07-24
        //String currentDate = "2013-02-27";
        Debug.println("currentDay:"+currentDate);
        StringBuilder sbff = new StringBuilder();
        Iterator it=listbanners.iterator();
        int count = 0;
        while(it.hasNext())
        {
            String key = (String)it.next();
            String view_hour    = getClickInDay(cassAct, currentDate, key, ColumFamlyBannerViewCountCf);
            String click_hour   = getClickInDay(cassAct, currentDate, key, ColumFamlyBannerClickCountCf);
            String str = "("+ key + "_" + click_hour + "_" + view_hour +"_'" + currentDate +"')";
            sbff.append(str).append("_");
            count ++ ;            
            if(count % 1000 == 0){
                System.out.println("count:"+count);
                //break;                
            }
        }

         //xu li cho du lieu cuoi
        if(sbff.toString().length() > 1)
        {                        
            String relStr = sbff.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));          
            HandleImportData hid = new HandleImportData();
            String pathFile="";
            
            try {
                pathFile = hid.outToCsvBannerHourly(valInsert);
                Debug.println("pathFile:"+pathFile);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            try {
                 //get sys_date_cpc
                String strDate =  mysql67.getSysDateCPC();              
                MysqlDataManager67 mysql672 = new MysqlDataManager67(false);
                mysql672.beforeLoadData();
                mysql672.dropPartition(CONSTANT.V3_BAN_HOURLY_CS, strDate);
                mysql672.loadToV3BannerHourlyFromFile(pathFile);
                mysql672.shutdownConnection();
                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        //close connection to DB
        mysql67.shutdownConnection();        
        
        long last_time = System.currentTimeMillis();
        System.out.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableBannerHour");

    }
    
    public static String getClickInDay(CassandraAction cassAct,String day,String key,String columnFamily) throws ParseException
    {

       StringBuilder sb = new StringBuilder();

       for(int i = 0 ; i < 24 ;i++)
       {
           if(i < 10)
           {
                if(i > 5){
                    String date = "0"+i+":00:00 "+ day;
                    long start_time = TimeUtil.longToDateWithHourBanner(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000 - 1 ;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                    sb.append(i).append("h:").append(countClick).append(",");
                } else {
                    String date = "0"+i+":00:00 "+ day;
                    long start_time = TimeUtil.longToDateWithHourBanner(date)+ 24*60*60*1000;
                    //them 1h
                    long finish_time = start_time + 60*60*1000 - 1 ;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                    sb.append(i).append("h:").append(countClick).append(",");
                }
           } else {
                String date = i+":00:00 " + day;
                 long start_time = TimeUtil.longToDateWithHourBanner(date);
                //them 1h
                long finish_time = start_time + 60*60*1000 - 1;
                long countClick = cassAct.getCountClickByIntervalTime(key, columnFamily, start_time, finish_time);
                if(i != 23)
                {
                    sb.append(i).append("h:").append(countClick).append(",");
                } else{
                    sb.append(i).append("h:").append(countClick);
                }
           }
       }
       return sb.toString();
    }
}
